package IBBB.model;

import javafx.beans.Observable;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;

import java.util.*;

public class UserMessage {
    private StringProperty name;
    // Map structure {wordStockName: [schedule, plan]}
    private ObservableMap<StringProperty, List<IntegerProperty>> wordStocks;

    public UserMessage() {

    }
    public UserMessage(String userName, Map<String, List<Integer>> userWordStock) {
        name = new SimpleStringProperty(userName);
        Map<StringProperty, List<IntegerProperty>> tempMap = new LinkedHashMap<>();
        if (userWordStock != null) {
            for (Map.Entry<String, List<Integer>> item : userWordStock.entrySet()) {
                List<IntegerProperty> tempList = new LinkedList<>();
                tempList.add(new SimpleIntegerProperty(item.getValue().get(0)));
                tempList.add(new SimpleIntegerProperty(item.getValue().get(1)));
                tempMap.put(new SimpleStringProperty(item.getKey()), tempList);
            }
        }
        wordStocks = FXCollections.observableMap(tempMap);
    }
    public final StringProperty getUserName() {
        return name;
    }

    public ObservableMap<StringProperty, List<IntegerProperty>> getWordStockInfo() {
        return wordStocks;
    }

    private StringProperty wordStockGetKey(String name) {
        for (StringProperty item: wordStocks.keySet()) {
            if (item.getValue().equals(name))
                return item;
        }
        return null;
    }

    public Integer getCurrentSchedule(String name) {
        StringProperty key = wordStockGetKey(name);
        return wordStocks.get(key).get(0).get();
    }

    public Integer getPlan(String name) {
        StringProperty key = wordStockGetKey(name);
        return wordStocks.get(key).get(1).get();
    }

    public boolean changeSchedule(String wordStockName, Integer plan) {
        if (wordStocks == null || wordStocks.size() == 0)
            return false;
        StringProperty key = wordStockGetKey(wordStockName);
        if (key != null) {
            List<IntegerProperty> temp = new LinkedList<>();
            temp.add(new SimpleIntegerProperty(wordStocks.get(key).get(0).get()));
            temp.add(new SimpleIntegerProperty(plan));
            wordStocks.replace(key, temp);
            return true;
        } else {
            return false;
        }
    }

    public boolean updateSchedule(String wordStockName, Integer num) {
        StringProperty key = wordStockGetKey(wordStockName);
        if (key == null) {
            return false;
        } else {
           List<IntegerProperty> temp = new ArrayList<>();
           IntegerProperty sche = wordStocks.get(key).get(0);
           IntegerProperty plan = wordStocks.get(key).get(1);
           temp.add(new SimpleIntegerProperty(sche.get() + num));
           temp.add(plan);
           wordStocks.replace(key, temp);
           return true;
        }
    }
    public boolean addSchedule(String wordStockName, Integer plan) {
        if (wordStocks == null) {
            wordStocks = FXCollections.emptyObservableMap();
        }
        StringProperty tempName = new SimpleStringProperty(wordStockName);
        StringProperty key = wordStockGetKey(wordStockName);
        if (key != null) {
            return false;
        } else {
            List<IntegerProperty> tempList = new LinkedList<>();
            IntegerProperty schedule = new SimpleIntegerProperty(0);
            IntegerProperty pl = new SimpleIntegerProperty(plan);
            tempList.add(schedule);
            tempList.add(pl);
            wordStocks.put(tempName, tempList);
            return true;
        }
    }

    public boolean deleteSchedule(String wordStockName) {
        if (wordStocks == null || wordStocks.size() == 0)
            return false;
        for (StringProperty item: wordStocks.keySet()) {
            if (item.getValue().equals(wordStockName)) {
                wordStocks.remove(item);
                return true;
            }
        }
        return false;
    }
// Test Code, turn them into notes when you don't need.
//    public static void main(String[] args) {
//        Map<String, List<Integer>> info = new HashMap<>();
//        List<Integer> schAndPl = new LinkedList<>();
//        schAndPl.add(15);
//        schAndPl.add(25);
//        info.put("CET4", schAndPl);
//        UserMessage testUser = new UserMessage("zhangxiaozheng", info);
//        Map<StringProperty, List<IntegerProperty>> words = testUser.getWordStockInfo();
//        for (Map.Entry<StringProperty, List<IntegerProperty>> item: words.entrySet()) {
//            System.out.println(item.getKey());
//            System.out.println(item.getValue().get(0));
//            System.out.println(item.getValue().get(1));
//        }
//        System.out.println();
//        testUser.addSchedule("CET6", 20);
//        for (Map.Entry<StringProperty, List<IntegerProperty>> item: words.entrySet()) {
//            System.out.println(item.getKey());
//            System.out.println(item.getValue().get(0));
//            System.out.println(item.getValue().get(1));
//        }
//        System.out.println();
//        if(testUser.addSchedule("CET4", 16)) {
//            System.out.println("suc add");
//        }
//
//
//        for (Map.Entry<StringProperty, List<IntegerProperty>> item: words.entrySet()) {
//            System.out.println(item.getKey());
//            System.out.println(item.getValue().get(0));
//            System.out.println(item.getValue().get(1));
//        }
//        if (testUser.deleteSchedule("CET4")) {
//            System.out.println("del suc");
//            for (Map.Entry<StringProperty, List<IntegerProperty>> item: words.entrySet()) {
//                System.out.println(item.getKey());
//                System.out.println(item.getValue().get(0));
//                System.out.println(item.getValue().get(1));
//            }
//        } else {
//            System.out.println("del fail");
//        }
//        if (testUser.deleteSchedule("CET4")) {
//            System.out.println("del suc");
//            for (Map.Entry<StringProperty, List<IntegerProperty>> item: words.entrySet()) {
//                System.out.println(item.getKey());
//                System.out.println(item.getValue().get(0));
//                System.out.println(item.getValue().get(1));
//            }
//        } else {
//            System.out.println("del fail");
//        }
//        if (testUser.changeSchedule("CET6", 50)) {
//            System.out.println("del suc");
//            for (Map.Entry<StringProperty, List<IntegerProperty>> item: words.entrySet()) {
//                System.out.println(item.getKey());
//                System.out.println(item.getValue().get(0));
//                System.out.println(item.getValue().get(1));
//            }
//        } else {
//            System.out.println("del fail");
//        }
//    }
}

